package org.csu.logistics.Controller;

import org.csu.logistics.Service.TransCenterService;
import org.csu.logistics.Service.WarehouseService;
import org.csu.logistics.common.CommonResponse;
import org.csu.logistics.entity.TransCenter;
import org.csu.logistics.entity.Warehouse;
import org.csu.logistics.vo.OrderOV;
import org.csu.logistics.vo.WarehouseOV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin(origins = "http://47.100.72.98:80")
@RequestMapping("/TransCenterController/")
public class TransCenterController {

    @Autowired
    TransCenterService transCenterService;

    @Autowired
    WarehouseService warehouseService;

    //接口19 添加转运中心
    @PostMapping("/addTransCenter")
    @CrossOrigin(origins = "http://47.100.72.98:80")
    @ResponseBody
    public CommonResponse<String> insert_transCenter(@Validated @RequestBody TransCenter transCenter){
        if(transCenterService.insert_transCenter(transCenter)==0){
            CommonResponse.createForError("插入失败");
        }
        return CommonResponse.createForSuccess("插入成功");
    }

    //接口20 查询所有转运中心
    @PostMapping("/checkTransCenter")
    @CrossOrigin(origins = "http://47.100.72.98:80")
    @ResponseBody
    public CommonResponse<List<TransCenter>> select_transCenterList(){
        List<TransCenter> transCenterList = transCenterService.select_transCenterList();
        if(transCenterList==null){
            return CommonResponse.createForError("没有仓库");
        }
        else{
            return CommonResponse.createForSuccess(transCenterList);
        }
    }

    //接口15 返回所有运输中订单，带转运节点状态
    @PostMapping("/checkOrder")
    @CrossOrigin(origins = "http://47.100.72.98:80")
    @ResponseBody
    public CommonResponse<List<WarehouseOV>> select_order(){
        return warehouseService.select_warehouse();
    }

    //接口15+ 返回订单中的orderNum不重复
    @PostMapping("/checkOrderNum")
    @CrossOrigin(origins = "http://47.100.72.98:80")
    @ResponseBody
    public CommonResponse<int[]> select_orderNum(){
        return warehouseService.select_orderNum();
    }

    //接口7 根据OrderNum查询转运节点状态
    @PostMapping("/checkOrderByOrderNum")
    @CrossOrigin(origins = "http://47.100.72.98:80")
    @ResponseBody
    public CommonResponse<List<WarehouseOV>> select_orderByOrderNum(@RequestBody Map<String,String> data){
        return warehouseService.select_warehouseOVByOrderNum(Integer.parseInt(data.get("orderNum")));
    }

    //接口14 根据转运中心查询转运节点状态
    @PostMapping("/checkOrderByLocale")
    @CrossOrigin(origins = "http://47.100.72.98:80")
    @ResponseBody
    public CommonResponse<List<WarehouseOV>> select_orderByLocale(@RequestBody Map<String,String> data){
        return warehouseService.select_warehouseOVByLocale(data.get("locale"));
    }

    //接口16 根据转运中心和订单号修改运输状态
    @PostMapping("/updateByLocaleAndOrderNum")
    @CrossOrigin(origins = "http://47.100.72.98:80")
    @ResponseBody
    public CommonResponse<String> update_localeByLocaleAndOrderNum(@RequestBody Map<String,String> data){
        Warehouse warehouse = new Warehouse();
        warehouse.setStatus(data.get("status"));
        warehouse.setOrderNum(Integer.parseInt(data.get("orderNum")));
        if(warehouseService.update_wareHouseByOrderNum(warehouse,data.get("locale"))==1){
            return CommonResponse.createForSuccess("状态更新成功");
        }
        return CommonResponse.createForError("状态更新失败");
    }
}
